package com.bgs.eat.operationType.service.impl;

import com.bgs.eat.config.CommonResult;
import com.bgs.eat.operationType.entity.OperationTypeEntity;
import com.bgs.eat.operationType.mapper.OperationTypeMapper;
import com.bgs.eat.operationType.service.OperationTypeService;
import com.bgs.eat.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName OperationTypeServiceImpl
 * @Description TODO
 * @Author lzy
 * @Date 2022/10/17 11:29
 **/
@Service
public class OperationTypeServiceImpl implements OperationTypeService {

    @Autowired
    OperationTypeMapper operationTypeMapper;

    @Autowired
    RedisUtil redisUtil;

    /**
    * @Description 申请店铺新增时获取 营业类别 信息列表
    * @Author lzy
    * @Date 2022/10/17 11:33
    * @Return com.bgs.eat.config.CommonResult

    */
    @Override
    public CommonResult getOperationList() {
        Object getOperationList = redisUtil.get("getOperationList");
        if(getOperationList != null && !getOperationList.equals("null")) {
            return CommonResult.ok(getOperationList);
        } else {
            OperationTypeEntity operationTypeEntity = new OperationTypeEntity();
            ArrayList<Object> operationParent = new ArrayList<>();
            ArrayList<Object> operationChilds = new ArrayList<>();
            operationTypeEntity.setPId(0);
            List<OperationTypeEntity> operationTypeEntities = operationTypeMapper.select(operationTypeEntity);
            OperationTypeEntity operationTypeEntity1 = operationTypeEntities.get(0);
            List<OperationTypeEntity> select = operationTypeMapper.getOperationListByPid(operationTypeEntity1.getId());
            operationParent.add(operationTypeEntities);
            operationParent.add(select);
            for (OperationTypeEntity typeEntity : operationTypeEntities) {
                List<OperationTypeEntity> operationListByPid = operationTypeMapper.getOperationListByPid(typeEntity.getId());
                operationChilds.add(operationListByPid);
            }
            HashMap<Object, Object> map = new HashMap<>();
            map.put("operationParent", operationParent);
            map.put("operationChilds", operationChilds);
            redisUtil.set("getOperationList", map);
            return CommonResult.ok(map);
        }
    }

    /**
     * @Author G胖
     * @Description 查询所有的营业类别
     * @Date 2022-10-26 上午 09:30
     * @Return com.bgs.eat.config.CommonResult

     */
    @Override
    public List<OperationTypeEntity> getAllOperationList() {
        OperationTypeEntity operationTypeEntity = new OperationTypeEntity();
        return operationTypeMapper.select(operationTypeEntity);
    }
}
